package org.example.rabbitmq.work;

import com.rabbitmq.client.*;

import org.example.rabbitmq.util.ConnectionUtil;

import java.io.IOException;

/**
 * @author Lenovo
 * @version 1.0
 * @description TODO
 * @date 25/7/2023 下午4:47
 */

public class Consumer1 {
    public static final String QUEUE_NAMQ = "worker_queue";
    public static void main(String[] args) throws Exception {
        // 获取连接
        Connection connection = ConnectionUtil.getConnection();
        // 创建信道
        Channel channel = connection.createChannel();
        // 创建队列
        channel.queueDeclare(QUEUE_NAMQ,true,false,false,null);

        // 创建Consumer对象
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                // 获取消费者唯一标识
                System.out.println("consumerTag = " + consumerTag);
                // 获取交换机的名字
                System.out.println("envelope.getExchange() = " + envelope.getExchange());
                // 获取路由key
                System.out.println("envelope.getRoutingKey() = " + envelope.getRoutingKey());
                // 获取当前是第几条消息
                System.out.println("envelope.getDeliveryTag() = " + envelope.getDeliveryTag());
                // 获取消息内容
                System.out.println("body："+new java.lang.String(body));
            }
        };
        // 消费消息
        channel.basicConsume(QUEUE_NAMQ,true,defaultConsumer);
    }
}
